{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "colonial-cooling",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "P OK\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "\u001b[32mimport \u001b[39m\u001b[36mscala.util.matching.Regex\n",
       "\n",
       "\u001b[39m\r\n",
       "\u001b[36mnumberPattern\u001b[39m: \u001b[32mRegex\u001b[39m = [0-9]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import scala.util.matching.Regex\n",
    "\n",
    "val numberPattern: Regex = \"[0-9]\".r\n",
    "\n",
    "numberPattern.findFirstMatchIn(\"awesomep1assword\") match {\n",
    "    case Some(_) => println(\"P OK\")\n",
    "    case None => println(\"P \")\n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "characteristic-answer",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "key: background-color value: #A03300\n",
      "key: background-image value: url(img\n",
      "key: background-position value: top center\n",
      "key: background-repeat value: repeat-x\n",
      "key: background-size value: 2160px 108px\n",
      "key: margin value: 0\n",
      "key: height value: 108px\n",
      "key: width value: 100\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "\u001b[32mimport \u001b[39m\u001b[36mscala.util.matching.Regex\n",
       "\n",
       "\u001b[39m\r\n",
       "\u001b[36mkeyValPattern\u001b[39m: \u001b[32mRegex\u001b[39m = ([0-9a-zA-Z-#() ]+): ([0-9a-zA-Z-#() ]+)\r\n",
       "\u001b[36minput\u001b[39m: \u001b[32mString\u001b[39m = \u001b[32m\"\"\"background-color: #A03300;\n",
       "background-image: url(img/header100.png);\n",
       "background-position: top center;\n",
       "background-repeat: repeat-x;\n",
       "background-size: 2160px 108px;\n",
       "margin: 0;\n",
       "height: 108px;\n",
       "width: 100%;\"\"\"\u001b[39m"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import scala.util.matching.Regex\n",
    "\n",
    "val keyValPattern: Regex = \"([0-9a-zA-Z-#() ]+): ([0-9a-zA-Z-#() ]+)\".r\n",
    "\n",
    "val input: String =\n",
    "  \"\"\"background-color: #A03300;\n",
    "    |background-image: url(img/header100.png);\n",
    "    |background-position: top center;\n",
    "    |background-repeat: repeat-x;\n",
    "    |background-size: 2160px 108px;\n",
    "    |margin: 0;\n",
    "    |height: 108px;\n",
    "    |width: 100%;\"\"\".stripMargin\n",
    "\n",
    "for (patternMatch <- keyValPattern.findAllMatchIn(input))\n",
    "  println(s\"key: ${patternMatch.group(1)} value: ${patternMatch.group(2)}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "palestinian-trick",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sukyoung\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "\u001b[32mimport \u001b[39m\u001b[36mscala.util.Random\n",
       "\n",
       "\u001b[39m\r\n",
       "defined \u001b[32mobject\u001b[39m \u001b[36mCustumerID\u001b[39m\r\n",
       "\u001b[36mcustumer1ID\u001b[39m: \u001b[32mString\u001b[39m = \u001b[32m\"Sukyoung--1405930944448636527\"\u001b[39m"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "//EXTRACTOR OBJECTS\n",
    "import scala.util.Random\n",
    "\n",
    "object CustumerID {\n",
    "    def apply(name:String) = s\"$name--${Random.nextLong}\"\n",
    "    \n",
    "    def unapply (custumerID:String):Option[String] = {\n",
    "        val stringArray: Array[String] = custumerID.split(\"--\")\n",
    "        if (stringArray.tail.nonEmpty) Some(stringArray.head) else None \n",
    "    }\n",
    "}\n",
    "\n",
    "val custumer1ID = CustumerID(\"Sukyoung\")\n",
    "\n",
    "custumer1ID match {\n",
    "    case CustumerID(name) => println (name)\n",
    "    case _ => println(\"could not extract a CustumerID\")\n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "accessible-privilege",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "xxx\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "\u001b[36mcustomer2ID\u001b[39m: \u001b[32mString\u001b[39m = \u001b[32m\"xxx--1175061926510094095\"\u001b[39m\r\n",
       "\u001b[36mname\u001b[39m: \u001b[32mString\u001b[39m = \u001b[32m\"xxx\"\u001b[39m"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val customer2ID = CustumerID(\"xxx\")\n",
    "val CustumerID(name) = customer2ID\n",
    "println(name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "transparent-compression",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[36mname\u001b[39m: \u001b[32mString\u001b[39m = \u001b[32m\"xxx\"\u001b[39m"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val name = CustumerID.unapply(customer2ID).get"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "geographic-speaking",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[36mname2\u001b[39m: \u001b[32mString\u001b[39m = \u001b[32m\"\"\u001b[39m"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val CustumerID(name2) = \"--asdfasdfasdf\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "forty-separate",
   "metadata": {},
   "outputs": [
    {
     "ename": "",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[31mscala.MatchError: -asdfasdfasdf (of class java.lang.String)\u001b[39m\r\n  ammonite.$sess.cmd7$Helper.<init>(\u001b[32mcmd7.sc\u001b[39m:\u001b[32m1\u001b[39m)\r\n  ammonite.$sess.cmd7$.<init>(\u001b[32mcmd7.sc\u001b[39m:\u001b[32m7\u001b[39m)\r\n  ammonite.$sess.cmd7$.<clinit>(\u001b[32mcmd7.sc\u001b[39m:\u001b[32m-1\u001b[39m)"
     ]
    }
   ],
   "source": [
    "val CustumerID(name3) = \"-asdfasdfasdf\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "banned-prototype",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Scala",
   "language": "scala",
   "name": "scala"
  },
  "language_info": {
   "codemirror_mode": "text/x-scala",
   "file_extension": ".scala",
   "mimetype": "text/x-scala",
   "name": "scala",
   "nbconvert_exporter": "script",
   "version": "2.11.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
